package com.oocl.o2o.admin.listener;

import java.io.IOException;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

import com.oocl.o2o.admin.entity.Merchant;
import com.oocl.o2o.admin.service.MerchantService;
import com.oocl.o2o.admin.util.Base64Unit;
import com.oocl.o2o.admin.util.FileUtil;
import com.oocl.o2o.connectutil.assist.ConfigUtil;
import com.oocl.o2o.connectutil.assist.JsonUtil;
import com.oocl.o2o.connectutil.dto.MerchantDTO;

public class MerchantRegisterJMSListener implements MessageListener {

	@Resource(name = "merchantServiceImpl")
	private MerchantService merchantService;
	private static int registerCount = 0;
	private static Logger logger = Logger
			.getLogger(MerchantRegisterJMSListener.class);

	@Override
	public void onMessage(Message message) {

		TextMessage textMsg = (TextMessage) message;
		try {
			registerCount++;
			logger.info("接收新的注册消息内容------");
			MerchantDTO merchantDTO = JsonUtil.getObjectFromJson(
					textMsg.getText(), MerchantDTO.class);
			merchantService.register(merchantDTO);
			message.acknowledge();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	public static int getRegisterCount() {
		return registerCount;
	}

	public static void setRegisterCount(int registerCount) {
		MerchantRegisterJMSListener.registerCount = registerCount;
	}

}
